package org.abratuhi.kvngroupware.brainstorm.mptua;

import java.util.List;
import java.util.Properties;

import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.model.Brainstorm;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.AbstractResponseMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class BrainstormResponseMessageProceeder extends AbstractResponseMessageProceeder{
	
	LoginManager loginmanager;
	BrainstormManager brainstormmanager;
	WidgetManager widgetmanager;
	
	public BrainstormResponseMessageProceeder(MessageSender sender, Properties locale, LoginManager loginmanager, BrainstormManager brainstormmanager, WidgetManager widgetmanager) {
		super(sender, locale);
		this.loginmanager = loginmanager;
		this.brainstormmanager = brainstormmanager;
		this.widgetmanager = widgetmanager;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");
		String brainstormId = response.getAttributeValue("brainstormId");
		String userId = response.getAttributeValue("userId");
		String teamId = response.getAttributeValue("teamId");
		String brainstormTheme = response.getAttributeValue("brainstrormTheme");
		String status = response.getAttributeValue("status");
		String text = response.getText();

		if(type.equals("brainstorm")){
			if(status.equals("ok")){
				if(action.equals("create")){
					brainstormmanager.put(brainstormId, new Brainstorm(brainstormId, teamId, brainstormTheme));
				}
				else if(action.equals("get")){
					List<Element> brainstorms = response.getChildren("brainstorm");
					brainstormmanager.clear();
					for(int i=0; i<brainstorms.size();i++) {
						Brainstorm brainstorm = Brainstorm.fromElement(brainstorms.get(i));
						brainstormmanager.put(brainstorm.getId(), brainstorm);
					}
				}
				else if(action.equals("delete")){
					brainstormmanager.remove(brainstormId);
				}
				else if(action.equals("start")){

				}
				else if(action.equals("stop")){

				}
				else if(action.equals("join")){
					if(brainstormmanager.get(brainstormId).containsMember(userId))
						brainstormmanager.get(brainstormId).addMember(userId);
					if(userId.equals(loginmanager.getUser().getId())){
						// TODO: create brainstorm window
					}
				}
				else if(action.equals("leave")){
					if(brainstormmanager.get(brainstormId).containsMember(userId))
						brainstormmanager.get(brainstormId).removeMember(userId);
					if(userId.equals(loginmanager.getUser().getId())){
						// TODO: dispose brainstorm window
					}
				}
				else if(action.equals("commit")){
					if(brainstormmanager.get(brainstormId).containsMember(userId))
						brainstormmanager.get(brainstormId).commitSheet(userId, text);
				}
				else if(action.equals("switch")){
					if(brainstormmanager.get(brainstormId).containsMember(userId))
						brainstormmanager.get(brainstormId).switchSheets();
				}
			}
		}
	}

}
